package com.fzz.learning_forum.controller;

import com.fzz.learning_forum.domain.Article;
import com.fzz.learning_forum.domain.Comment;
import com.fzz.learning_forum.domain.Reply;
import com.fzz.learning_forum.service.ArticleService;
import com.fzz.learning_forum.service.CommentService;
import com.fzz.learning_forum.serviceImpl.ArticleServiceImpl;
import com.fzz.learning_forum.serviceImpl.CommentServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CommentController {


    @Autowired
    private CommentService commentService=new CommentServiceImpl();

    @Autowired
    private ArticleService articleService=new ArticleServiceImpl();

    //发表评论
    @RequestMapping(value = "/user/publishComment")
    @ResponseBody
    public String publishComment(Comment comment)
    {
        Integer rows = commentService.publishComment(comment);
        System.out.println(comment);
        if(rows>0)
        {
            try {
                Article article = articleService.selectArticleDetail(comment.getArticle_id());
                Integer hit = article.getHit();
                hit+=1;
                article.setHit(hit);
                articleService.updateArticleHit(comment.getArticle_id(),hit);
            }
            catch (Exception e)
            {
                System.out.println(e.getCause());
            }
            return "OK";
        }
        return "NO";
    }


    //查询我评论过的动态
    @RequestMapping(value = "/user/myCommentArticle/{p}/{id}")
    public String myCommentArticle(HttpServletRequest request,@PathVariable("p") Integer p,@PathVariable("id") Integer id, @RequestParam(value = "count", defaultValue = "3")Integer count) {
        PageInfo<Article> articleList = commentService.myCommentArticle(id,p,count);
        HttpSession session = request.getSession();
        session.setAttribute("articles", articleList);
        List<Article> hitList = articleService.selectArticleByHit();
        session.setAttribute("hitList",hitList);
        return "client/myCommArti";
    }
}
